package com.person.title.service.impl;

import java.util.List;

import com.person.commons.utils.DAO;
import com.person.sys.model.SysFunc;
import com.person.sys.model.SysMenu;
import com.person.title.model.Title;
import com.person.title.model.TitleClass;
import com.person.title.model.TitleSub;
import com.person.title.service.TitleService;

public class TitleServiceImpl implements TitleService {

	private DAO dao;
	private static final char SUCCESS = '1';
	private static final char FAIL = '0';
	private static final char FAIL_1 = '2';//主栏目分类代码重复的提示返回
	private static final char FAIL_2 = '3';//主栏目分类名字重复的提示返回
	private static final char FAIL_3 = '4';//主栏目分类名字重复的提示返回

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}
	
	
	
	@Override
	public char createTitle(Title title) {
		
		//主菜单分类代码是否重复
		char forward = 0;		
		String hql1 = "from Title where titleCode = '"+title.getTitleCode()+"'";
		List<Title> list1 = dao.find(hql1);
		
		//主菜单名字是否重复
		String hql2 = "from Title where titleName = '"+title.getTitleName()+"'";
		List<Title>  list2 = dao.find(hql2);
		
		//主菜单英文名字是否重复
		String hql3 = "from Title where titleNameEn = '"+title.getTitleNameEn()+"'";
		List<Title>  list3 = dao.find(hql3);
		if(list1.size() == 0 && list2.size() == 0 && list3.size() == 0){			
			this.dao.save(title);
			forward = SUCCESS;
		}else{
			if(list1.size() > 0){
				forward = FAIL_1;
			}else if(list2.size() > 0){
				forward = FAIL_2;
			}else if(list3.size() > 0){
				forward = FAIL_3;
			}
		}
		return forward;
	}
	
	

	@Override
	public char deleteTitle(Title title) {
		
		String hql = "from TitleSub where titleCodeGroup like '" + title.getTitleCode() + "%'";
	    List<SysFunc> list = dao.find(hql);
	    if(list.size() == 0){
	    	this.dao.delete(title) ;	
	    	return SUCCESS;
	    }else{
	    	
	    	return FAIL;
	    }	
	}

	@Override
	public List<Title> findByCode(String titleCode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Title findSysTitleId(String titleId) {
		
		return (Title) this.dao.find(Title.class, titleId) ;
	}


	@Override
	public char modifyTitle(Title title) {
		
		char forward = 0;
		
		//主菜单分类代码是否重复
		String hql1 = "from Title where titleCode = '"+title.getTitleCode()+"' and titleId != '"+title.getTitleId()+"'";
		List<Title> list1 = dao.find(hql1);
				
		//主菜单名字是否重复
		String hql2 = "from Title where titleName = '"+title.getTitleName()+"' and titleId != '"+title.getTitleId()+"'";
		List<Title>  list2 = dao.find(hql2);
		
		//主菜单名字是否重复
		String hql3 = "from Title where titleNameEn = '"+title.getTitleNameEn()+"' and titleId != '"+title.getTitleId()+"'";
		List<Title>  list3 = dao.find(hql3);
		
		if(list1.size() == 0 && list2.size() == 0&& list3.size() == 0){
			
			dao.update(title);
			
			forward = SUCCESS;
		}else{
			if(list1.size() > 0){
				forward = FAIL_1;
			}else if(list2.size() > 0){
				forward = FAIL_2;
			}else if(list3.size() > 0){
				forward = FAIL_3;
			}
		}
		return forward;
	}

	@Override
	public List<Title> titleList() {		
		return this.dao.find("from Title where 1=1 order by titleCode") ;
	}

	@Override
	public List<TitleSub> juniorTitleList(String titleId) {

		Title title = (Title) dao.find(Title.class, titleId) ;	
		String hql = "from TitleSub where titleCodeGroup like '" + title.getTitleCode() + "__' order by titleCodeGroup " ;
		List <TitleSub> titleSubs = this.dao.find(hql) ;
	
		return titleSubs;
	}

	
	@Override
	public char modifyJuniorTitleSub(Title title, String titleCode) {
		String hql = "from TitleSub where titleCodeGroup like '" + titleCode + "__%' order by titleCodeGroup " ;
		List <TitleSub> titleSubs = this.dao.find(hql) ;
		for (TitleSub titleSub:titleSubs) {
			String titleCodeGroup = titleSub.getTitleCodeGroup() ;
			String titleCodeGroup1 = title.getTitleCode() + titleCodeGroup.substring(titleCode.length(), titleCodeGroup.length()) ;
			titleSub.setTitleCodeGroup(titleCodeGroup1) ;
			this.dao.update(titleSub) ;
		}
		return SUCCESS ;
	}
	
	
	@Override
	public Title findTitleByCode(String titleCode) {
		String hql = "from Title where titleCode = '" + titleCode+ "'" ;
		List <Title> titles = this.dao.find(hql) ;
		Title title = null ;
		if (titles.size()>0) {
			title = (Title) titles.get(0) ;
		}
		return title ;
	}
	public Title findTitleByTitleCodeGroup(String titleCodeGroup){
		String str=titleCodeGroup.substring(0,2);
		try{
			return (Title)dao.find("from Title where titleCode='"+str+"'").get(0);
		}catch(Exception e){
			e.printStackTrace();
			return new Title();
		}
	}
	public void update(Title title){
		dao.update(title);
	}
	
	
	
	

}
